home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Networking / Network Stream / TNetworkStream.h < prev   
Encoding:
Text File  |  1996-05-28  |  2.6 KB  |  105 lines  |  [TEXT/CWIE]

  1. //    TNetworkStream.h - Macintosh OpenTransport Network Stream IO class object
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinne Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #ifndef _H_TNETWORKSTREAM
  18. #define _H_TNETWORKSTREAM
  19.  
  20. #include <iostream.h>
  21. #include <OpenTransport.h>
  22.  
  23. //
  24. // TNetworkBuf  - Macintosh OpenTransport Network Stream Buffer class object
  25. //
  26.  
  27. #define BUFFSIZE 128
  28.  
  29. class TNetworkBuf :  public streambuf {
  30.  
  31. //     CONSTRUCTORS AND DESTRUCTORS
  32. public:
  33.         TNetworkBuf() : streambuf(),
  34.                         fEndPoint(kOTInvalidRef),
  35.                         fBuffer(nil),
  36.                         fChunkSize(0) { };
  37.         ~TNetworkBuf();
  38.         
  39. // HIGH LEVEL FUNCTIONS
  40. public:
  41.     void    attach (EndpointRef ep, TEndpointInfo* info);
  42.     void    close();
  43.     int        is_open() const { return (fEndPoint != (EndpointRef)kOTInvalidRef) ; }
  44.  
  45. // CLASS MEMBERS
  46. public:
  47.     static     void    Release(void *);
  48.     
  49.             char*    ReserveBuffer(size_t reqCount, size_t  *actCount);
  50.             void    EnqueueBuffer(char* buf, size_t count);
  51.             
  52.  
  53. // PROTECTED FUNCTIONS
  54. protected :
  55.     virtual int     overflow( int = EOF);
  56.     virtual int     sync();
  57.     virtual int     xsputn( const char*, int);
  58.     
  59.     virtual int     underflow();
  60.     virtual int     uflow();
  61.         
  62. // PRIVATE FUNCTIONS
  63. private:    
  64.             int        flush_output(OTFlags flags = 0);
  65.         Boolean        doallocate();
  66.  
  67. // PRIVATE FIELDS
  68. private:
  69.     EndpointRef        fEndPoint;            // network endpoint
  70.     TEndpointInfo*     fInfo;                // pointer to endpoint Info Stucture
  71.     char*            fBuffer;            // begining of buffer area
  72.     size_t            fChunkSize;            // default chunksize
  73.     
  74.     char            Buffer[BUFFSIZE];
  75. };
  76.  
  77. //
  78. // TNetworkOStream  - Macintosh OpenTransport Network Stream output class object
  79. //
  80.  
  81. class TNetworkIOStream :  
  82.                     virtual public TNetworkBuf, 
  83.                     virtual public ostream, 
  84.                     virtual public istream {
  85.  
  86. //     CONSTRUCTORS AND DESTRUCTORS
  87.     public:
  88.         TNetworkIOStream (): 
  89.                             ostream((TNetworkBuf*) this),
  90.                             istream((TNetworkBuf*) this),
  91.                             ios(0) {};
  92.                             
  93.         TNetworkIOStream (EndpointRef ep, TEndpointInfo* info):
  94.                                  ostream((TNetworkBuf*) this),
  95.                                  istream((TNetworkBuf*) this),
  96.                                  ios(0) 
  97.                                  { attach(ep, info); };
  98.    ~TNetworkIOStream();
  99.  
  100. };
  101.  
  102.  
  103.  
  104. #endif
  105.